package com.vvlive.caibao.smartcity.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.maps2d.AMapUtils;
import com.amap.api.maps2d.model.LatLng;
import com.vvlive.caibao.smartcity.base.BaseActivity;
import com.vvlive.caibao.smartcity.bean.Image;
import com.vvlive.caibao.smartcity.view.CommonActionBar;
import com.vvlive.caibao.smartcity.view.CommonActionBar.OnMoreListener;
import com.vvlive.caibao.smartcity.view.FlowLayout;
import com.vvlive.caibao.smartcity.view.selector.MultiImageSelectorActivity;

import org.kymjs.kjframe.ui.BindView;

import java.util.List;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by zhj on 2015/7/2.
 * 发表朋友圈页面
 */
public class PublishOneMomentActivity extends BaseActivity implements OnMoreListener {

    /**
     * 显示已选择的图片的宽度
     */
    private final static int IMAGE_WIDTH = 100;
    /**
     * 显示已选择的图片的宽度
     */
    private final static int IMAGE_HEIGHT = 100;

    @BindView(id = R.id.pom_input)
    EditText contentInput;   // 发表的文字内容

    @BindView(id = R.id.pom_images_layout)
    FlowLayout imagesLayout;
    @BindView(id = R.id.pom_add_image, click = true)
    ImageView addImage; // 添加图片
    @BindView(id = R.id.pom_choose_location, click = true)
    RelativeLayout chooseLocation;  // 选择地点
    @BindView(id = R.id.pom_show_location)
    TextView showLocation;  // 显示地点

    private final int PICK_IMAGES_REQUEST_CODE = 3;

    @Override
    protected int getContentViewRes() {
        return R.layout.activity_publish_one_moment;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        initActionBar();
    }

    private void initActionBar() {
        CommonActionBar actionBar = getCommonActionBar();
        actionBar.setTitleVisible(false);

        actionBar.setBackView(getBackView());
        actionBar.setMoreView(getMoreView());

        actionBar.setOnMoreListener(this);
    }

    private View getMoreView() {
        return getTextViewByText("发送");
    }

    private View getBackView() {
        return getTextViewByText("取消");
    }

    /**
     * 通过给定的文本信息产生一个TextView
     *
     * @param text 给定的文本信息
     * @return
     */
    private TextView getTextViewByText(String text){
        TextView textView = new TextView(this);
        textView.setText(text);
        textView.setTextColor(Color.WHITE);
        textView.setTextSize(20);
        textView.setGravity(Gravity.CENTER);
        return textView;
    }

    @Override
    public void widgetClick(View v) {
        super.widgetClick(v);
        switch (v.getId()){
            case R.id.pom_add_image:    // 添加图片
                startActivityForResult(new Intent(this, MultiImageSelectorActivity.class), PICK_IMAGES_REQUEST_CODE);
                break;
            case R.id.pom_choose_location:  // 选择位置
                toast("选择位置");
                break;
        }
    }

    @Override
    public void onMore(View v) {
        // content, pictures, location;
        String content = contentInput.getText().toString(); // get the content;
        String location = showLocation.getText().toString();    // get the location;

        toast(content + "\n" + location);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == PICK_IMAGES_REQUEST_CODE && resultCode == Activity.RESULT_OK){
            List<String> selectedPath = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
            imagesLayout.removeAllViews();
            for (String path : selectedPath){
                imagesLayout.addView(getImageViewByPath(path));
            }
        }
    }

    /**
     * 根据图片的路径获得ImageView
     *
     * @param path  图片路径
     * @return  要得到的ImageView
     */
    private ImageView getImageViewByPath(String path){
        ImageView imageView = new ImageView(this);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new ViewGroup.LayoutParams(80, 80));
        imageView.setImageBitmap(BitmapFactory.decodeFile(path));
        return imageView;
    }
}
